#ifndef TESTBOARD_BOARD_HPP
#define TESTBOARD_BOARD_HPP

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <cerrno>
#include <ctime>
#include <mutex>
#include <queue>
#include <vector>
#include <memory>
#include <string>
#include <thread>
#include <serial_driver/serial_driver.hpp>
// 定义枚举类型
enum class PacketControllerState : uint8_t {
    PACKET_CONTROLLER_STATE_STARTBYTE1 = 0,
    PACKET_CONTROLLER_STATE_STARTBYTE2 = 1,
    PACKET_CONTROLLER_STATE_LENGTH = 2,
    PACKET_CONTROLLER_STATE_FUNCTION = 3,
    PACKET_CONTROLLER_STATE_ID = 4,
    PACKET_CONTROLLER_STATE_DATA = 5,
    PACKET_CONTROLLER_STATE_CHECKSUM = 6
};

enum class PacketFunction : uint8_t {
    PACKET_FUNC_SYS = 0,
    PACKET_FUNC_LED = 1,
    PACKET_FUNC_BUZZER = 2,
    PACKET_FUNC_MOTOR = 3,
    PACKET_FUNC_PWM_SERVO = 4,
    PACKET_FUNC_BUS_SERVO = 5,
    PACKET_FUNC_KEY = 6,
    PACKET_FUNC_IMU = 7,
    PACKET_FUNC_GAMEPAD = 8,
    PACKET_FUNC_SBUS = 9,
    PACKET_FUNC_NONE = 10
};

enum class PacketReportKeyEvents : uint8_t {
    KEY_EVENT_PRESSED = 0x01,
    KEY_EVENT_LONGPRESS = 0x02,
    KEY_EVENT_LONGPRESS_REPEAT = 0x04,
    KEY_EVENT_RELEASE_FROM_LP = 0x08,
    KEY_EVENT_RELEASE_FROM_SP = 0x10,
    KEY_EVENT_CLICK = 0x20,
    KEY_EVENT_DOUBLE_CLICK = 0x40,
    KEY_EVENT_TRIPLE_CLICK = 0x80
};

// CRC8表
const uint8_t crc8_table[] = {
    0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
    157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
    35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
    190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
    70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
    219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
    101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
    248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
    140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
    17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
    175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
    50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
    202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
    87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
    233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
    116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};

// 计算CRC8校验和
uint8_t checksum_crc8(const std::vector<uint8_t>& data) {
    uint8_t check = 0;
    for (uint8_t b : data) {
        check = crc8_table[check ^ b];
    }
    return check & 0x00FF;
}

// SBus状态结构体
struct SBusStatus {
    std::vector<int16_t> channels = std::vector<int16_t>(16, 0);
    bool channel_17 = false;
    bool channel_18 = false;
    bool signal_loss = true;
    bool fail_safe = false;
};

class Board {
public:
    // 构造函数
    Board::Board(const std::string &device_port, int baudrate, int timeout_ms);
    // 析构函数
    ~Board();

    void enable_reception(); // 启动接收线程
    uint16_t get_battery(); 
    std::pair<uint8_t, uint8_t> get_button();
    std::vector<float> get_imu();
    std::pair<std::vector<float>, std::vector<int>> get_gamepad(); 
    std::vector<float> get_sbus();
    void set_led(float on_time, float off_time, uint8_t repeat = 1, uint8_t led_id = 1);
    void set_buzzer(uint16_t freq, float on_time, float off_time, uint8_t repeat = 1);
    void set_motor_speed(const std::vector<std::pair<uint8_t, float>>& speeds);
    void pwm_servo_set_position(float duration, const std::vector<std::pair<uint8_t, uint16_t>>& positions);
    void pwm_servo_set_offset(uint8_t servo_id, int8_t offset);
    int8_t pwm_servo_read_offset(uint8_t servo_id);
    uint16_t pwm_servo_read_position(uint8_t servo_id); 
    void bus_servo_enable_torque(uint8_t servo_id, bool enable);
    void bus_servo_set_id(uint8_t servo_id_now, uint8_t servo_id_new); 
    void bus_servo_set_offset(uint8_t servo_id, int8_t offset);
    void bus_servo_save_offset(uint8_t servo_id); 
    void bus_servo_set_angle_limit(uint8_t servo_id, const std::pair<uint16_t, uint16_t>& limit); 
    void bus_servo_set_vin_limit(uint8_t servo_id, const std::pair<uint16_t, uint16_t>& limit);
    void bus_servo_set_temp_limit(uint8_t servo_id, int8_t limit); 
    void bus_servo_stop(const std::vector<uint8_t>& servo_id);
    void bus_servo_set_position(float duration, const std::vector<std::pair<uint8_t, uint16_t>>& positions); 
    std::vector<int> bus_servo_read_and_unpack(uint8_t servo_id, uint8_t cmd, const std::string& unpack); 
    int bus_servo_read_id(uint8_t servo_id = 254);
    int bus_servo_read_offset(uint8_t servo_id); 
    int bus_servo_read_position(uint8_t servo_id);
    int bus_servo_read_vin(uint8_t servo_id);
    int bus_servo_read_temp(uint8_t servo_id); 
    int bus_servo_read_temp_limit(uint8_t servo_id);
    std::pair<int, int> bus_servo_read_angle_limit(uint8_t servo_id); 
    std::pair<int, int> bus_servo_read_vin_limit(uint8_t servo_id); 
    int bus_servo_read_torque_state(uint8_t servo_id); 



private:
    
    std::shared_ptr<drivers::serial_driver::SerialDriver> serial_driver_ ;
    std::string device_port;             
    int baudrate_; 
    int timeout_ms;
    bool enable_recv = false;

    std::queue<std::vector<uint8_t>> sys_queue;
    std::queue<std::vector<uint8_t>> key_queue;
    std::queue<std::vector<uint8_t>> imu_queue;
    std::queue<std::vector<uint8_t>> gamepad_queue;
    std::queue<std::vector<uint8_t>> bus_servo_queue;
    std::queue<std::vector<uint8_t>> pwm_servo_queue;
    std::queue<std::vector<uint8_t>> sbus_queue;

    std::mutex pwm_servo_read_lock;
    std::mutex servo_read_lock;

    // 接收任务线程函数
    void recv_task();
    void packet_report_sys(const std::vector<uint8_t>& data);//更新电量
    void packet_report_key(const std::vector<uint8_t>& data); //更新按键
    void packet_report_imu(const std::vector<uint8_t>& data); //更新IMU
    void packet_report_gamepad(const std::vector<uint8_t>& data);//更新游戏手柄
    void packet_report_serial_servo(const std::vector<uint8_t>& data);//更新串口舵机
    void packet_report_pwm_servo(const std::vector<uint8_t>& data);//更新PWM舵机
    void packet_report_sbus(const std::vector<uint8_t>& data);//更新SBUS
    void buf_write(PacketFunction func, const std::vector<uint8_t>& data);//发送数据包
};

#endif // TESTBOARD_BOARD_HPP